<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/29
 * Time: 17:29
 */

namespace Common\Model;


use Think\Model;

class Permissions extends Model
{
    protected $tableName = 'permissions';
    public $id;
    public $name;
    public $display_name;
    public $description;
    public $updated_at;
    public $created_at;
    public $isQuery = false;

    public function __construct($id)
    {
        parent::__construct();
        $this->id = $id;
        $this->init();
    }

    private function init() {
        $result = $this->where('id = ' . $this->id)->select();
        if (!empty($result)) {
            $this->isQuery = true;
            $this->id = $result[0]['id'];
            $this->name = $result[0]['name'];
            $this->display_name = $result[0]['display_name'];
            $this->description = $result[0]['description'];
            $this->updated_at = $result[0]['updated_at'];
            $this->created_at = $result[0]['created_at'];
        } else {
            $this->isQuery = false;
        }
    }

    /**
     * 创建 permission
     *
     * @param array $data
     * @return mixed
     */
    public static function creates(array $data)
    {
        return M('permissions')->add($data);
    }

    /**
     * 更新Permission
     *
     * @param int $id
     * @param array $data
     * @return bool
     */
    public static function update($id, array $data)
    {
        return M('permissions')->where('id = ' . $id)->save($data);
    }
}